package com.garlic.face.jlf.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.R;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.balance.DesUtil;

/**
 * Created by shiyanqi on 18/9/19.
 */

public class SuperVisorConfirmDialog extends Dialog implements View.OnClickListener {

	private static final int INPUT_ACCOUNT = 1;
	private static final int INPUT_PASSWORD = 2;
	private int type = INPUT_ACCOUNT;
	private String account = "";
	private String psw = "";
	private String pswMask = "";
	private Context context;
	private ImageView closeBtn;
	private TextView titleName;
	private TextView done;
	private ImageView img;
	private TextView num_0;
	private TextView num_1;
	private TextView num_2;
	private TextView num_3;
	private TextView num_4;
	private TextView num_5;
	private TextView num_6;
	private TextView num_7;
	private TextView num_8;
	private TextView num_9;
	private TextView num_delete;
	private EditText inputBarCode;
	private EditText hideBar;
	private CallBack callBack;

	public SuperVisorConfirmDialog(@NonNull Context context) {
		super(context);
		this.context = context;
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		View view = View.inflate(context, R.layout.dialog_super_visor_confirm, null);
		setContentView(view);
		closeBtn = view.findViewById(R.id.close);
		titleName = view.findViewById(R.id.title_name);
		done = view.findViewById(R.id.num_done);
		inputBarCode = view.findViewById(R.id.input_bar_code);
		inputBarCode.setText("");
		num_0 = view.findViewById(R.id.num_0);
		num_1 = view.findViewById(R.id.num_1);
		num_2 = view.findViewById(R.id.num_2);
		num_3 = view.findViewById(R.id.num_3);
		num_4 = view.findViewById(R.id.num_4);
		num_5 = view.findViewById(R.id.num_5);
		num_6 = view.findViewById(R.id.num_6);
		num_7 = view.findViewById(R.id.num_7);
		num_8 = view.findViewById(R.id.num_8);
		num_9 = view.findViewById(R.id.num_9);
		num_delete = view.findViewById(R.id.num_delete);
		hideBar = findViewById(R.id.hide_bar);
		hideBar.setFocusable(true);
		inputBarCode.setInputType(InputType.TYPE_NULL);
		hideBar.setInputType(InputType.TYPE_NULL);
		closeBtn.setOnClickListener(this);
		done.setOnClickListener(this);
		num_0.setOnClickListener(this);
		num_1.setOnClickListener(this);
		num_2.setOnClickListener(this);
		num_3.setOnClickListener(this);
		num_4.setOnClickListener(this);
		num_5.setOnClickListener(this);
		num_6.setOnClickListener(this);
		num_7.setOnClickListener(this);
		num_8.setOnClickListener(this);
		num_9.setOnClickListener(this);
		num_delete.setOnClickListener(this);
		hideBar.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
				if (getWindow()!=null)
					getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void afterTextChanged(Editable editable) {
				String end = editable.toString();
				if (end.contains("#")) {
					String msg = DesUtil.decryptPassword(end.substring(2, end.length() - 1), Means.secretKey);
					callBack.finish(msg);
					hideBar.setText("");
				}
			}
		});

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.close:
				type = INPUT_ACCOUNT;
				callBack.close(view);
				break;
			case R.id.num_done:
				confirmDone();
				break;
			case R.id.num_0:
				appendTextView(inputBarCode, "0");
				break;
			case R.id.num_1:
				appendTextView(inputBarCode, "1");
				break;
			case R.id.num_2:
				appendTextView(inputBarCode, "2");
				break;
			case R.id.num_3:
				appendTextView(inputBarCode, "3");
				break;
			case R.id.num_4:
				appendTextView(inputBarCode, "4");
				break;
			case R.id.num_5:
				appendTextView(inputBarCode, "5");
				break;
			case R.id.num_6:
				appendTextView(inputBarCode, "6");
				break;
			case R.id.num_7:
				appendTextView(inputBarCode, "7");
				break;
			case R.id.num_8:
				appendTextView(inputBarCode, "8");
				break;
			case R.id.num_9:
				appendTextView(inputBarCode, "9");
				break;
			case R.id.num_delete:
				inputBarCode.setText("");
				if(type == INPUT_ACCOUNT){
					account = "";
				}else {
					psw = "";
					pswMask = "";
				}
				break;
			default:
				break;
		}
	}

	private void confirmDone() {
		if(type == INPUT_ACCOUNT){
			account = inputBarCode.getText().toString().trim();
			titleName.setText("请输入管理员密码");
			inputBarCode.setText("");
			type = INPUT_PASSWORD;
		}else {
			type = INPUT_ACCOUNT;
			callBack.done(account, psw);
			account = "";
			psw = "";
			pswMask = "";
		}
	}

	private void appendTextView(EditText tv, String append) {
		if(type == INPUT_ACCOUNT){
			String old = tv.getText().toString();
			old += append;
			tv.setText(old);
			tv.setSelection(old.length());
		}else {
			psw += append;
			pswMask += "＊";
			tv.setText(pswMask);
			tv.setSelection(pswMask.length());
		}
	}

	public void setCallBack(CallBack callBack){
		this.callBack = callBack;
	}


	public interface CallBack {
		void close(View view);

		void done(String account, String psw);

		void finish(String account_psw);
	}

}
